<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>转换字符串第一个字节为 0-255 之间的值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.number-format.html">« number_format</a></li>
      <li style="float: right;"><a href="function.parse-str.html">parse_str »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>转换字符串第一个字节为 0-255 之间的值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.ord" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ord</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ord</span> &mdash; <span class="dc-title">转换字符串第一个字节为 0-255 之间的值</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.ord-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ord</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$character</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   解析 <code class="parameter">character</code> 二进制值第一个字节为 0 到 255 范围的无符号整型类型。
  </p>
  <p class="para">
   如果字符串是 ASCII、 ISO-8859、Windows 1252之类单字节编码，就等于返回该字符在字符集编码表中的位置。
   但请注意，本函数不会去检测字符串的编码，尤其是不会识别类似 UTF-8 或 UTF-16 这种多字节字符的 Unicode 代码点（code point）。
  </p>
  <p class="para">
   该函数是 <span class="function"><a href="function.chr.html" class="function">chr()</a></span> 的互补函数。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.ord-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">character</code></dt>

     <dd>

      <p class="para">
       一个字符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.ord-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回 0 - 255 的整型值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.ord-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4792">
    <p><strong>示例 #1 <span class="function"><strong>ord()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">ord</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"The&nbsp;first&nbsp;character&nbsp;of&nbsp;\$str&nbsp;is&nbsp;a&nbsp;line&nbsp;feed.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
 </p>
 <p class="para"> 
   <div class="example" id="example-4793">
    <p><strong>示例 #2 检查 UTF-8 字符串的每一个字节</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">declare(</span><span style="color: #0000BB">encoding</span><span style="color: #007700">=</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"🐘"</span><span style="color: #007700">;<br />for&nbsp;(&nbsp;</span><span style="color: #0000BB">$pos</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">++&nbsp;)&nbsp;{<br />&nbsp;</span><span style="color: #0000BB">$byte&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pos</span><span style="color: #007700">);<br />&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Byte&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'&nbsp;of&nbsp;$str&nbsp;has&nbsp;value&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">ord</span><span style="color: #007700">(</span><span style="color: #0000BB">$byte</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen"><br />
Byte 0 of $str has value 240<br />
Byte 1 of $str has value 159<br />
Byte 2 of $str has value 144<br />
Byte 3 of $str has value 152<br />
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.ord-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.chr.html" class="function" rel="rdfs-seeAlso">chr()</a> - 从数字生成单字节字符串</span></li>
    <li class="member"><a href="https://www.man7.org/linux/man-pages/man7/ascii.7.html" class="link external">&raquo;&nbsp;ASCII 码表</a></li>
    <li class="member"><span class="function"><a href="function.mb-ord.html" class="function" rel="rdfs-seeAlso">mb_ord()</a> - 获取字符的 Unicode 码位值</span></li>
    <li class="member"><span class="function"><a href="intlchar.ord.html" class="function" rel="rdfs-seeAlso">IntlChar::ord()</a> - Return Unicode code point value of character</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>